<?php

namespace App\Transformers\StoreCoupon;

use App\Models\StoreCouponIssue;
use App\Transformers\User\UserTransformer;
use League\Fractal\TransformerAbstract;

class StoreCouponIssueTransformer extends TransformerAbstract
{
    public $availableIncludes = ['users'];

    public function transform(StoreCouponIssue $data)
    {
        return [
            'id' => $data->id,
            'product_id' => $data->product_id ?? '',
            'category_id' => $data->category_id ?? '',
            'total_count' => $data->total_count ?? '',
            'remain_count' => $data->remain_count ?? '',
            'full_reduction' => $data->full_reduction ?? '',
            'title' => $data->title ?? '',
            'integral' => $data->integral ?? '',
            'coupon_price' => $data->coupon_price ?? '',
            'threshold' => $data->threshold($data),
            'use_min_price' => $data->use_min_price ?? '',
            'usagetime' => $data->usagetime($data),
            'coupon_time' => $data->coupon_time ?? '',
            'status' => $data->status ?? '',
            'is_full_give' => $data->is_full_give ?? '',
            'is_give_subscribe' => $data->is_give_subscribe ?? '',
            'is_permanent' => $data->is_permanent ?? '',
            'limitquantity' => $data->limitquantity,
            'num' => $data->sum($data),
            'type' => $data->type ?? '',
            'type_text' => $data->receiveType[$data->type] ?? '',
            'receive_type' => $data->receive_type ?? '',
            'receive_type_text' => $data->receiveType[$data->receive_type] ?? '',
            'start_use_time' => $data->start_use_time ?? '',
            'end_use_time' => $data->end_use_time ?? '',
            'collection_date' => $data->collection_date($data),
            'sort' => $data->sort ?? '',
            'collectiontime' => $data->collectiontime($data),
            'start_time' => $data->start_time ?? '',
            'end_time' => $data->end_time ?? '',
            'date_of_use' => $data->date_of_use($data),
            'created_at' => $data->created_at ? $data->created_at->format("Y-m-d H:i:s") : '',
            'updated_at' => $data->updated_at ? $data->updated_at->format("Y-m-d H:i:s") : '',
        ];
    }

    public function includeUsers(StoreCouponIssue $couponIssue)
    {
        return $this->collection($couponIssue->users(), new UserTransformer());
    }
}
